// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import store from './store/index'
import App from './App'
import router from './router'
// import Navigation from 'vue-navigation'
import JzoAuth from '@/common/js/common/JzoAuth'
import Constants from '@/common/js/common/Constants'
import EcHttpUtils from '@/common/js/EcHttpUtils'
import 'vue-layer-mobile/need/layer.css'
import layer from 'vue-layer-mobile'
import Lockr from 'lockr'
import WxUtils from './common/js/common/WxUtils'
import VueLazyLoad from 'vue-lazyload'
// 自定义组件
import Plugins from './components/plugins/common.js'
Vue.use(Plugins)
Vue.use(VueLazyLoad, {
  error: 'https://testapp.jzdoctor.com/ec-web/static/img/defimg.jpg',
  loading: 'https://testapp.jzdoctor.com/ec-web/static/img/defimg.jpg'
})
Vue.use(require('vue-wechat-title'))
Vue.use(layer)
Vue.prototype.$EcHttpUtils = EcHttpUtils
Vue.prototype.$Lockr = Lockr
Vue.prototype.$JzoAuth = JzoAuth
Vue.prototype.$Constants = Constants
Vue.prototype.$WxUtils = WxUtils
Vue.prototype.$loadingStart = function () {
  let loadingW = document.getElementById('page-loading')
  loadingW.style.display = 'block'
  setTimeout(() => {
    loadingW.classList.remove('hide')
  }, 200)
}
Vue.prototype.$loadingEnd = function () {
  let loadingW = document.getElementById('page-loading')
  loadingW.classList.add('hide')
  setTimeout(() => {
    loadingW.style.display = 'none'
  }, 500)
}
require('!style-loader!css-loader!less-loader!./assets/css/main.less')
/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  template: '<App/>',
  beforeCreate: function () {
    // 判断支付环境 100 微信 200 支付宝 300 其他浏览器
    let ua = window.navigator.userAgent.toLowerCase()
    Vue.prototype.$payWay = 300
    if (ua.match(/MicroMessenger/i) && ua.match(/MicroMessenger/i).toString() === 'micromessenger') {
      Vue.prototype.$payWay = 100
    }
    if (ua.match(/AlipayClient/i) && ua.match(/AlipayClient/i).toString() === 'alipayclient') {
      Vue.prototype.$payWay = 200
    }
    // Vue.prototype.$payWay = 200
    // 支付宝环境不缓存 + 微信环境
    if (Vue.prototype.$payWay !== 100) {
      JzoAuth.aliyoauth()
    }
    if (Vue.prototype.$payWay === 200) {
      // JzoAuth.aliyoauth()
      // JzoAuth.aliyoauth({
      //   appid: Constants.CommonApiUrl.aliPayAppid,
      //   app_type: Constants.CommonApiUrl.aliPayApp_type
      // })
    } else {
      // 写死测试环境
      // JzoAuth.oauth({
      //   appid: Constants.APPID,
      //   app_type: Constants.APP_TYPE.WECHAT_SERVICE,
      //   unionid: 'o5WoIuIF8M9BOfEy0xRnQcs1Wgh8'
      // })
      // 正式环境
      // if (window.localStorage.getItem(JzoAuth.oAuthPropsCacheKey) == null || window.localStorage.getItem(JzoAuth.oAuthPropsCacheKey) === '') {
      //   if (!this.$route.query.unionid || this.$route.query.unionid === null || this.$route.query.unionid === '') {
      //     let redirectUrl = encodeURIComponent(window.location.href)
      //     window.location.href = Constants.CommonApiUrl.OATUTH + '/authorize?redirect_url=' + redirectUrl + '&appid=' + Constants.APPID + '&bind=false'
      //   } else {
      //     // console.log(this.$route.query.unionid)
      //     JzoAuth.oauth({
      //       // appid: 'wx5968535a8293a98c', // 正式
      //       // appid: 'wxb7761aa9905ffb33', // 测试
      //       appid: Constants.APPID, // 测试
      //       app_type: Constants.APP_TYPE.WECHAT_SERVICE,
      //       unionid: this.$route.query.unionid
      //     })
      //   }
      // } else {
      //   JzoAuth.oauth()
      // }
      WxUtils.init()
    }
    // 测试用333333
    // let href = window.location.href
    // var url = href.indexOf('version')
    // var urls = href.replace(location.hash, '')
    // var u = urls.split('?')
    // console.log(u)
    // if (url === -1) {
    //   var newhref = u.length === 1 ? u[0] + '?version=' + new Date().getTime() : u[0] + '?' + u[1] + '&version=' + new Date().getTime()
    //   window.location.href = newhref
    // } else {
    //   // u.splice(1)
    //   // var newhrefss = u.length === 1 ? u[0] + '?version=' + new Date().getTime() : u[0] + '?' + u[1] + '&version=' + new Date().getTime()
    //   console.log(window.location.href)
    //   // window.location.href = newhrefss
    // }
    // JzoAuth.oauth({
    //   appid: Constants.APPID,
    //   app_type: Constants.APP_TYPE.WECHAT_SERVICE,
    //   unionid: 'o5WoIuIF8M9BOfEy0xRnQcs1Wgh8'
    // })
    // console.log('初始化微信')
    // // console.log(this.$store.state.serverIP)
    // if (localStorage.getItem('JZoAuthPropsCacheKey')) {
    //   var unid = localStorage.getItem('JZoAuthPropsCacheKey')
    //   if (JSON.parse(unid).unionid === 'o5WoIuIF8M9BOfEy0xRnQcs1Wgh8') {
    //     window.location.href = 'https://app.jzdoctor.com/ec-web/clear.html'
    //   }
    // }
  },
  created: function () {
    let title = this.$route.meta.title
    if (!title) {
      title = '线下支付'
    }
    document.title = title
   /* this.$WxUtils.share(Constants.CommonApiUrl.AURL + '/ec-web/', '彩虹优选', '专业，让妈妈更轻松！')
    this.$WxUtils.shareToFriend(Constants.CommonApiUrl.AURL + '/ec-web/', '彩虹优选', '专业，让妈妈更轻松！') */
  },
  watch: {
    '$route' (to, form) {
      // if (to.path === '/admin/login' || to.path === '/admin/list') {
      //   console.log(33)
      //   if (!Lockr.get('shopids')) {
      //     this.$router.replace({name: 'Login'})
      //   }
      // }
      // if（) {
      // }
    }
  },
  components: { App }
})
